lib/tsocket: add tsocket_guide.txt section with specific details about bsd style...
[ira/wip.git] / lib / tsocket / tsocket_readv.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 2009
5
6      ** NOTE! The following LGPL license applies to the tevent
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/network.h"
26 #include "tsocket.h"
27 #include "tsocket_internal.h"
28
29 struct tsocket_readv_state {
30         /* this structs are owned by the caller */
31         struct {
32                 struct tsocket_context *sock;
33                 tsocket_readv_next_iovec_t next_iovec_fn;
34                 void *private_data;
35         } caller;
36
37         /*
38          * Each call to the callback resets iov and count
39          * the callback allocated the iov as child of our state,
40          * that means we are allowed to modify and free it.
41          *
42          * we should call the callback every time we filled the given
43          * vector and ask for a new vector. We return if the callback
44          * ask for 0 bytes.
45          */
46         struct iovec *iov;
47         size_t count;
48
49         /*
50          * the total number of bytes we read,
51          * the return value of the _recv function
52          */
53         int total_read;
54 };
55
56 static int tsocket_readv_state_destructor(struct tsocket_readv_state *state)
57 {
58         if (state->caller.sock) {
59                 tsocket_set_readable_handler(state->caller.sock, NULL, NULL);
60         }
61         ZERO_STRUCT(state->caller);
62
63         return 0;
64 }
65
66 static bool tsocket_readv_ask_for_next_vector(struct tevent_req *req,
67                                               struct tsocket_readv_state *state)
68 {
69         int ret;
70         int err;
71         bool dummy;
72         size_t to_read = 0;
73         size_t i;
74
75         talloc_free(state->iov);
76         state->iov = NULL;
77         state->count = 0;
78
79         ret = state->caller.next_iovec_fn(state->caller.sock,
80                                           state->caller.private_data,
81                                           state, &state->iov, &state->count);
82         err = tsocket_error_from_errno(ret, errno, &dummy);
83         if (tevent_req_error(req, err)) {
84                 return false;
85         }
86
87         for (i=0; i < state->count; i++) {
88                 size_t tmp = to_read;
89                 tmp += state->iov[i].iov_len;
90
91                 if (tmp < to_read) {
92                         tevent_req_error(req, EMSGSIZE);
93                         return false;
94                 }
95
96                 to_read = tmp;
97         }
98
99         if (to_read == 0) {
100                 tevent_req_done(req);
101                 return false;
102         }
103
104         if (state->total_read + to_read < state->total_read) {
105                 tevent_req_error(req, EMSGSIZE);
106                 return false;
107         }
108
109         return true;
110 }
111
112 static void tsocket_readv_handler(struct tsocket_context *sock,
113                                   void *private_data);
114
115 struct tevent_req *tsocket_readv_send(struct tsocket_context *sock,
116                                       TALLOC_CTX *mem_ctx,
117                                       tsocket_readv_next_iovec_t next_iovec_fn,
118                                       void *private_data)
119 {
120         struct tevent_req *req;
121         struct tsocket_readv_state *state;
122         int ret;
123         int err;
124         bool dummy;
125
126         req = tevent_req_create(mem_ctx, &state,
127                                 struct tsocket_readv_state);
128         if (!req) {
129                 return NULL;
130         }
131
132         state->caller.sock              = sock;
133         state->caller.next_iovec_fn     = next_iovec_fn;
134         state->caller.private_data      = private_data;
135
136         state->iov              = NULL;
137         state->count            = 0;
138         state->total_read       = 0;
139
140         if (!tsocket_readv_ask_for_next_vector(req, state)) {
141                 goto post;
142         }
143
144         talloc_set_destructor(state, tsocket_readv_state_destructor);
145
146         ret = tsocket_set_readable_handler(sock,
147                                            tsocket_readv_handler,
148                                            req);
149         err = tsocket_error_from_errno(ret, errno, &dummy);
150         if (tevent_req_error(req, err)) {
151                 goto post;
152         }
153
154         return req;
155
156  post:
157         return tevent_req_post(req, sock->event.ctx);
158 }
159
160 static void tsocket_readv_handler(struct tsocket_context *sock,
161                                   void *private_data)
162 {
163         struct tevent_req *req = talloc_get_type(private_data,
164                                  struct tevent_req);
165         struct tsocket_readv_state *state = tevent_req_data(req,
166                                             struct tsocket_readv_state);
167         ssize_t ret;
168         int err;
169         bool retry;
170
171         ret = tsocket_readv(state->caller.sock,
172                             state->iov,
173                             state->count);
174         err = tsocket_error_from_errno(ret, errno, &retry);
175         if (retry) {
176                 /* retry later */
177                 return;
178         }
179         if (tevent_req_error(req, err)) {
180                 return;
181         }
182
183         state->total_read += ret;
184
185         while (ret > 0) {
186                 if (ret < state->iov[0].iov_len) {
187                         uint8_t *base;
188                         base = (uint8_t *)state->iov[0].iov_base;
189                         base += ret;
190                         state->iov[0].iov_base = base;
191                         state->iov[0].iov_len -= ret;
192                         break;
193                 }
194                 ret -= state->iov[0].iov_len;
195                 state->iov += 1;
196                 state->count -= 1;
197         }
198
199         if (state->count) {
200                 /* we have more to read */
201                 return;
202         }
203
204         /* ask the callback for a new vector we should fill */
205         tsocket_readv_ask_for_next_vector(req, state);
206 }
207
208 int tsocket_readv_recv(struct tevent_req *req, int *perrno)
209 {
210         struct tsocket_readv_state *state = tevent_req_data(req,
211                                             struct tsocket_readv_state);
212         int ret;
213
214         ret = tsocket_simple_int_recv(req, perrno);
215         if (ret == 0) {
216                 ret = state->total_read;
217         }
218
219         tevent_req_received(req);
220         return ret;
221 }
222