]> arthur.barton.de Git - netatalk.git/blob - libatalk/tsocket/tsocket_helpers.c
Merge master
[netatalk.git] / libatalk / tsocket / tsocket_helpers.c
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif /* HAVE_CONFIG_H */
27
28 #include <atalk/tsocket.h>
29
30 #include "tsocket_internal.h"
31
32 struct tdgram_sendto_queue_state {
33         /* this structs are owned by the caller */
34         struct {
35                 struct tevent_context *ev;
36                 struct tdgram_context *dgram;
37                 const uint8_t *buf;
38                 size_t len;
39                 const struct tsocket_address *dst;
40         } caller;
41         ssize_t ret;
42 };
43
44 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
45                                          void *private_data);
46 static void tdgram_sendto_queue_done(struct tevent_req *subreq);
47
48 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
49                                             struct tevent_context *ev,
50                                             struct tdgram_context *dgram,
51                                             struct tevent_queue *queue,
52                                             const uint8_t *buf,
53                                             size_t len,
54                                             struct tsocket_address *dst)
55 {
56         struct tevent_req *req;
57         struct tdgram_sendto_queue_state *state;
58         bool ok;
59
60         req = tevent_req_create(mem_ctx, &state,
61                                 struct tdgram_sendto_queue_state);
62         if (!req) {
63                 return NULL;
64         }
65
66         state->caller.ev        = ev;
67         state->caller.dgram     = dgram;
68         state->caller.buf       = buf;
69         state->caller.len       = len;
70         state->caller.dst       = dst;
71         state->ret              = -1;
72
73         ok = tevent_queue_add(queue,
74                               ev,
75                               req,
76                               tdgram_sendto_queue_trigger,
77                               NULL);
78         if (!ok) {
79                 tevent_req_nomem(NULL, req);
80                 goto post;
81         }
82
83         return req;
84
85  post:
86         tevent_req_post(req, ev);
87         return req;
88 }
89
90 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
91                                          void *private_data)
92 {
93         struct tdgram_sendto_queue_state *state = tevent_req_data(req,
94                                         struct tdgram_sendto_queue_state);
95         struct tevent_req *subreq;
96
97         subreq = tdgram_sendto_send(state,
98                                     state->caller.ev,
99                                     state->caller.dgram,
100                                     state->caller.buf,
101                                     state->caller.len,
102                                     state->caller.dst);
103         if (tevent_req_nomem(subreq, req)) {
104                 return;
105         }
106         tevent_req_set_callback(subreq, tdgram_sendto_queue_done, req);
107 }
108
109 static void tdgram_sendto_queue_done(struct tevent_req *subreq)
110 {
111         struct tevent_req *req = tevent_req_callback_data(subreq,
112                                  struct tevent_req);
113         struct tdgram_sendto_queue_state *state = tevent_req_data(req,
114                                         struct tdgram_sendto_queue_state);
115         ssize_t ret;
116         int sys_errno;
117
118         ret = tdgram_sendto_recv(subreq, &sys_errno);
119         talloc_free(subreq);
120         if (ret == -1) {
121                 tevent_req_error(req, sys_errno);
122                 return;
123         }
124         state->ret = ret;
125
126         tevent_req_done(req);
127 }
128
129 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno)
130 {
131         struct tdgram_sendto_queue_state *state = tevent_req_data(req,
132                                         struct tdgram_sendto_queue_state);
133         ssize_t ret;
134
135         ret = tsocket_simple_int_recv(req, perrno);
136         if (ret == 0) {
137                 ret = state->ret;
138         }
139
140         tevent_req_received(req);
141         return ret;
142 }
143
144 struct tstream_readv_pdu_state {
145         /* this structs are owned by the caller */
146         struct {
147                 struct tevent_context *ev;
148                 struct tstream_context *stream;
149                 tstream_readv_pdu_next_vector_t next_vector_fn;
150                 void *next_vector_private;
151         } caller;
152
153         /*
154          * Each call to the callback resets iov and count
155          * the callback allocated the iov as child of our state,
156          * that means we are allowed to modify and free it.
157          *
158          * we should call the callback every time we filled the given
159          * vector and ask for a new vector. We return if the callback
160          * ask for 0 bytes.
161          */
162         struct iovec *vector;
163         size_t count;
164
165         /*
166          * the total number of bytes we read,
167          * the return value of the _recv function
168          */
169         int total_read;
170 };
171
172 static void tstream_readv_pdu_ask_for_next_vector(struct tevent_req *req);
173 static void tstream_readv_pdu_readv_done(struct tevent_req *subreq);
174
175 struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
176                                 struct tevent_context *ev,
177                                 struct tstream_context *stream,
178                                 tstream_readv_pdu_next_vector_t next_vector_fn,
179                                 void *next_vector_private)
180 {
181         struct tevent_req *req;
182         struct tstream_readv_pdu_state *state;
183
184         req = tevent_req_create(mem_ctx, &state,
185                                 struct tstream_readv_pdu_state);
186         if (!req) {
187                 return NULL;
188         }
189
190         state->caller.ev                        = ev;
191         state->caller.stream                    = stream;
192         state->caller.next_vector_fn            = next_vector_fn;
193         state->caller.next_vector_private       = next_vector_private;
194
195         state->vector           = NULL;
196         state->count            = 0;
197         state->total_read       = 0;
198
199         tstream_readv_pdu_ask_for_next_vector(req);
200         if (!tevent_req_is_in_progress(req)) {
201                 goto post;
202         }
203
204         return req;
205
206  post:
207         return tevent_req_post(req, ev);
208 }
209
210 static void tstream_readv_pdu_ask_for_next_vector(struct tevent_req *req)
211 {
212         struct tstream_readv_pdu_state *state = tevent_req_data(req,
213                                             struct tstream_readv_pdu_state);
214         int ret;
215         size_t to_read = 0;
216         size_t i;
217         struct tevent_req *subreq;
218
219         TALLOC_FREE(state->vector);
220         state->count = 0;
221
222         ret = state->caller.next_vector_fn(state->caller.stream,
223                                            state->caller.next_vector_private,
224                                            state, &state->vector, &state->count);
225         if (ret == -1) {
226                 tevent_req_error(req, errno);
227                 return;
228         }
229
230         if (state->count == 0) {
231                 tevent_req_done(req);
232                 return;
233         }
234
235         for (i=0; i < state->count; i++) {
236                 size_t tmp = to_read;
237                 tmp += state->vector[i].iov_len;
238
239                 if (tmp < to_read) {
240                         tevent_req_error(req, EMSGSIZE);
241                         return;
242                 }
243
244                 to_read = tmp;
245         }
246
247         /*
248          * this is invalid the next vector function should have
249          * reported count == 0.
250          */
251         if (to_read == 0) {
252                 tevent_req_error(req, EINVAL);
253                 return;
254         }
255
256         if (state->total_read + to_read < state->total_read) {
257                 tevent_req_error(req, EMSGSIZE);
258                 return;
259         }
260
261         subreq = tstream_readv_send(state,
262                                     state->caller.ev,
263                                     state->caller.stream,
264                                     state->vector,
265                                     state->count);
266         if (tevent_req_nomem(subreq, req)) {
267                 return;
268         }
269         tevent_req_set_callback(subreq, tstream_readv_pdu_readv_done, req);
270 }
271
272 static void tstream_readv_pdu_readv_done(struct tevent_req *subreq)
273 {
274         struct tevent_req *req = tevent_req_callback_data(subreq,
275                                  struct tevent_req);
276         struct tstream_readv_pdu_state *state = tevent_req_data(req,
277                                             struct tstream_readv_pdu_state);
278         int ret;
279         int sys_errno;
280
281         ret = tstream_readv_recv(subreq, &sys_errno);
282         if (ret == -1) {
283                 tevent_req_error(req, sys_errno);
284                 return;
285         }
286
287         state->total_read += ret;
288
289         /* ask the callback for a new vector we should fill */
290         tstream_readv_pdu_ask_for_next_vector(req);
291 }
292
293 int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno)
294 {
295         struct tstream_readv_pdu_state *state = tevent_req_data(req,
296                                             struct tstream_readv_pdu_state);
297         int ret;
298
299         ret = tsocket_simple_int_recv(req, perrno);
300         if (ret == 0) {
301                 ret = state->total_read;
302         }
303
304         tevent_req_received(req);
305         return ret;
306 }
307
308 struct tstream_readv_pdu_queue_state {
309         /* this structs are owned by the caller */
310         struct {
311                 struct tevent_context *ev;
312                 struct tstream_context *stream;
313                 tstream_readv_pdu_next_vector_t next_vector_fn;
314                 void *next_vector_private;
315         } caller;
316         int ret;
317 };
318
319 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
320                                          void *private_data);
321 static void tstream_readv_pdu_queue_done(struct tevent_req *subreq);
322
323 struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
324                                 struct tevent_context *ev,
325                                 struct tstream_context *stream,
326                                 struct tevent_queue *queue,
327                                 tstream_readv_pdu_next_vector_t next_vector_fn,
328                                 void *next_vector_private)
329 {
330         struct tevent_req *req;
331         struct tstream_readv_pdu_queue_state *state;
332         bool ok;
333
334         req = tevent_req_create(mem_ctx, &state,
335                                 struct tstream_readv_pdu_queue_state);
336         if (!req) {
337                 return NULL;
338         }
339
340         state->caller.ev                        = ev;
341         state->caller.stream                    = stream;
342         state->caller.next_vector_fn            = next_vector_fn;
343         state->caller.next_vector_private       = next_vector_private;
344         state->ret                              = -1;
345
346         ok = tevent_queue_add(queue,
347                               ev,
348                               req,
349                               tstream_readv_pdu_queue_trigger,
350                               NULL);
351         if (!ok) {
352                 tevent_req_nomem(NULL, req);
353                 goto post;
354         }
355
356         return req;
357
358  post:
359         return tevent_req_post(req, ev);
360 }
361
362 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
363                                          void *private_data)
364 {
365         struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
366                                         struct tstream_readv_pdu_queue_state);
367         struct tevent_req *subreq;
368
369         subreq = tstream_readv_pdu_send(state,
370                                         state->caller.ev,
371                                         state->caller.stream,
372                                         state->caller.next_vector_fn,
373                                         state->caller.next_vector_private);
374         if (tevent_req_nomem(subreq, req)) {
375                 return;
376         }
377         tevent_req_set_callback(subreq, tstream_readv_pdu_queue_done ,req);
378 }
379
380 static void tstream_readv_pdu_queue_done(struct tevent_req *subreq)
381 {
382         struct tevent_req *req = tevent_req_callback_data(subreq,
383                                  struct tevent_req);
384         struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
385                                         struct tstream_readv_pdu_queue_state);
386         int ret;
387         int sys_errno;
388
389         ret = tstream_readv_pdu_recv(subreq, &sys_errno);
390         talloc_free(subreq);
391         if (ret == -1) {
392                 tevent_req_error(req, sys_errno);
393                 return;
394         }
395         state->ret = ret;
396
397         tevent_req_done(req);
398 }
399
400 int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno)
401 {
402         struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
403                                         struct tstream_readv_pdu_queue_state);
404         int ret;
405
406         ret = tsocket_simple_int_recv(req, perrno);
407         if (ret == 0) {
408                 ret = state->ret;
409         }
410
411         tevent_req_received(req);
412         return ret;
413 }
414
415 struct tstream_writev_queue_state {
416         /* this structs are owned by the caller */
417         struct {
418                 struct tevent_context *ev;
419                 struct tstream_context *stream;
420                 const struct iovec *vector;
421                 size_t count;
422         } caller;
423         int ret;
424 };
425
426 static void tstream_writev_queue_trigger(struct tevent_req *req,
427                                          void *private_data);
428 static void tstream_writev_queue_done(struct tevent_req *subreq);
429
430 struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
431                                              struct tevent_context *ev,
432                                              struct tstream_context *stream,
433                                              struct tevent_queue *queue,
434                                              const struct iovec *vector,
435                                              size_t count)
436 {
437         struct tevent_req *req;
438         struct tstream_writev_queue_state *state;
439         bool ok;
440
441         req = tevent_req_create(mem_ctx, &state,
442                                 struct tstream_writev_queue_state);
443         if (!req) {
444                 return NULL;
445         }
446
447         state->caller.ev        = ev;
448         state->caller.stream    = stream;
449         state->caller.vector    = vector;
450         state->caller.count     = count;
451         state->ret              = -1;
452
453         ok = tevent_queue_add(queue,
454                               ev,
455                               req,
456                               tstream_writev_queue_trigger,
457                               NULL);
458         if (!ok) {
459                 tevent_req_nomem(NULL, req);
460                 goto post;
461         }
462
463         return req;
464
465  post:
466         return tevent_req_post(req, ev);
467 }
468
469 static void tstream_writev_queue_trigger(struct tevent_req *req,
470                                          void *private_data)
471 {
472         struct tstream_writev_queue_state *state = tevent_req_data(req,
473                                         struct tstream_writev_queue_state);
474         struct tevent_req *subreq;
475
476         subreq = tstream_writev_send(state,
477                                      state->caller.ev,
478                                      state->caller.stream,
479                                      state->caller.vector,
480                                      state->caller.count);
481         if (tevent_req_nomem(subreq, req)) {
482                 return;
483         }
484         tevent_req_set_callback(subreq, tstream_writev_queue_done ,req);
485 }
486
487 static void tstream_writev_queue_done(struct tevent_req *subreq)
488 {
489         struct tevent_req *req = tevent_req_callback_data(subreq,
490                                  struct tevent_req);
491         struct tstream_writev_queue_state *state = tevent_req_data(req,
492                                         struct tstream_writev_queue_state);
493         int ret;
494         int sys_errno;
495
496         ret = tstream_writev_recv(subreq, &sys_errno);
497         talloc_free(subreq);
498         if (ret == -1) {
499                 tevent_req_error(req, sys_errno);
500                 return;
501         }
502         state->ret = ret;
503
504         tevent_req_done(req);
505 }
506
507 int tstream_writev_queue_recv(struct tevent_req *req, int *perrno)
508 {
509         struct tstream_writev_queue_state *state = tevent_req_data(req,
510                                         struct tstream_writev_queue_state);
511         int ret;
512
513         ret = tsocket_simple_int_recv(req, perrno);
514         if (ret == 0) {
515                 ret = state->ret;
516         }
517
518         tevent_req_received(req);
519         return ret;
520 }
521