2 Unix SMB/CIFS implementation.
3 Utilities around tsocket
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
23 #include "lib/util_tsock.h"
24 #include "../lib/tsocket/tsocket.h"
25 #include "../lib/util/tevent_unix.h"
27 struct tstream_read_packet_state {
28 struct tevent_context *ev;
29 struct tstream_context *stream;
30 ssize_t (*more)(uint8_t *buf, size_t buflen, void *private_data);
36 static void tstream_read_packet_done(struct tevent_req *subreq);
38 struct tevent_req *tstream_read_packet_send(TALLOC_CTX *mem_ctx,
39 struct tevent_context *ev,
40 struct tstream_context *stream,
42 ssize_t (*more)(uint8_t *buf,
47 struct tevent_req *req, *subreq;
48 struct tstream_read_packet_state *state;
50 req = tevent_req_create(mem_ctx, &state,
51 struct tstream_read_packet_state);
55 state->buf = talloc_array(state, uint8_t, initial);
56 if (tevent_req_nomem(state->buf, req)) {
57 return tevent_req_post(req, ev);
59 state->iov.iov_base = (void *)state->buf;
60 state->iov.iov_len = initial;
63 state->stream = stream;
65 state->private_data = private_data;
67 subreq = tstream_readv_send(state, ev, stream, &state->iov, 1);
68 if (tevent_req_nomem(subreq, req)) {
69 return tevent_req_post(req, ev);
71 tevent_req_set_callback(subreq, tstream_read_packet_done, req);
76 static void tstream_read_packet_done(struct tevent_req *subreq)
78 struct tevent_req *req = tevent_req_callback_data(
79 subreq, struct tevent_req);
80 struct tstream_read_packet_state *state = tevent_req_data(
81 req, struct tstream_read_packet_state);
87 ret = tstream_readv_recv(subreq, &err);
93 tevent_req_error(req, err);
97 if (state->more == NULL) {
98 /* Nobody to ask, this is a async read_data */
102 total = talloc_array_length(state->buf);
104 more = state->more(state->buf, total, state->private_data);
106 /* We got an invalid packet, tell the caller */
107 tevent_req_error(req, EIO);
111 /* We're done, full packet received */
112 tevent_req_done(req);
116 if (total + more < total) {
117 tevent_req_error(req, EMSGSIZE);
121 tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
122 if (tevent_req_nomem(tmp, req)) {
127 state->iov.iov_base = (void *)(state->buf + total);
128 state->iov.iov_len = more;
130 subreq = tstream_readv_send(state, state->ev, state->stream,
132 if (tevent_req_nomem(subreq, req)) {
135 tevent_req_set_callback(subreq, tstream_read_packet_done, req);
138 ssize_t tstream_read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
139 uint8_t **pbuf, int *perrno)
141 struct tstream_read_packet_state *state =
142 tevent_req_data(req, struct tstream_read_packet_state);
144 if (tevent_req_is_unix_error(req, perrno)) {
147 *pbuf = talloc_move(mem_ctx, &state->buf);
148 return talloc_array_length(*pbuf);