2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
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/network.h"
27 #include "tsocket_internal.h"
29 struct tsocket_readv_state {
30 /* this structs are owned by the caller */
32 struct tsocket_context *sock;
33 tsocket_readv_next_iovec_t next_iovec_fn;
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.
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
50 * the total number of bytes we read,
51 * the return value of the _recv function
56 static int tsocket_readv_state_destructor(struct tsocket_readv_state *state)
58 if (state->caller.sock) {
59 tsocket_set_readable_handler(state->caller.sock, NULL, NULL);
61 ZERO_STRUCT(state->caller);
66 static bool tsocket_readv_ask_for_next_vector(struct tevent_req *req,
67 struct tsocket_readv_state *state)
75 talloc_free(state->iov);
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)) {
87 for (i=0; i < state->count; i++) {
89 tmp += state->iov[i].iov_len;
92 tevent_req_error(req, EMSGSIZE);
100 tevent_req_done(req);
104 if (state->total_read + to_read < state->total_read) {
105 tevent_req_error(req, EMSGSIZE);
112 static void tsocket_readv_handler(struct tsocket_context *sock,
115 struct tevent_req *tsocket_readv_send(struct tsocket_context *sock,
117 tsocket_readv_next_iovec_t next_iovec_fn,
120 struct tevent_req *req;
121 struct tsocket_readv_state *state;
126 req = tevent_req_create(mem_ctx, &state,
127 struct tsocket_readv_state);
132 state->caller.sock = sock;
133 state->caller.next_iovec_fn = next_iovec_fn;
134 state->caller.private_data = private_data;
138 state->total_read = 0;
140 if (!tsocket_readv_ask_for_next_vector(req, state)) {
144 talloc_set_destructor(state, tsocket_readv_state_destructor);
146 ret = tsocket_set_readable_handler(sock,
147 tsocket_readv_handler,
149 err = tsocket_error_from_errno(ret, errno, &dummy);
150 if (tevent_req_error(req, err)) {
157 return tevent_req_post(req, sock->event.ctx);
160 static void tsocket_readv_handler(struct tsocket_context *sock,
163 struct tevent_req *req = talloc_get_type(private_data,
165 struct tsocket_readv_state *state = tevent_req_data(req,
166 struct tsocket_readv_state);
171 ret = tsocket_readv(state->caller.sock,
174 err = tsocket_error_from_errno(ret, errno, &retry);
179 if (tevent_req_error(req, err)) {
183 state->total_read += ret;
186 if (ret < state->iov[0].iov_len) {
188 base = (uint8_t *)state->iov[0].iov_base;
190 state->iov[0].iov_base = base;
191 state->iov[0].iov_len -= ret;
194 ret -= state->iov[0].iov_len;
200 /* we have more to read */
204 /* ask the callback for a new vector we should fill */
205 tsocket_readv_ask_for_next_vector(req, state);
208 int tsocket_readv_recv(struct tevent_req *req, int *perrno)
210 struct tsocket_readv_state *state = tevent_req_data(req,
211 struct tsocket_readv_state);
214 ret = tsocket_simple_int_recv(req, perrno);
216 ret = state->total_read;
219 tevent_req_received(req);