Merge commit 'release-4-0-0alpha15' into master4-tmp
[ira/wip.git] / source3 / lib / util_tsock.c
1 /*
2    Unix SMB/CIFS implementation.
3    Utilities around tsocket
4    Copyright (C) Volker Lendecke 2009
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/tsocket/tsocket.h"
22 #include "../lib/util/tevent_unix.h"
23
24 struct tstream_read_packet_state {
25         struct tevent_context *ev;
26         struct tstream_context *stream;
27         ssize_t (*more)(uint8_t *buf, size_t buflen, void *private_data);
28         void *private_data;
29         uint8_t *buf;
30         struct iovec iov;
31 };
32
33 static void tstream_read_packet_done(struct tevent_req *subreq);
34
35 struct tevent_req *tstream_read_packet_send(TALLOC_CTX *mem_ctx,
36                                             struct tevent_context *ev,
37                                             struct tstream_context *stream,
38                                             size_t initial,
39                                             ssize_t (*more)(uint8_t *buf,
40                                                             size_t buflen,
41                                                             void *private_data),
42                                             void *private_data)
43 {
44         struct tevent_req *req, *subreq;
45         struct tstream_read_packet_state *state;
46
47         req = tevent_req_create(mem_ctx, &state,
48                                 struct tstream_read_packet_state);
49         if (req == NULL) {
50                 return NULL;
51         }
52         state->buf = talloc_array(state, uint8_t, initial);
53         if (tevent_req_nomem(state->buf, req)) {
54                 return tevent_req_post(req, ev);
55         }
56         state->iov.iov_base = (void *)state->buf;
57         state->iov.iov_len = initial;
58
59         state->ev = ev;
60         state->stream = stream;
61         state->more = more;
62         state->private_data = private_data;
63
64         subreq = tstream_readv_send(state, ev, stream, &state->iov, 1);
65         if (tevent_req_nomem(subreq, req)) {
66                 return tevent_req_post(req, ev);
67         }
68         tevent_req_set_callback(subreq, tstream_read_packet_done, req);
69
70         return req;
71 }
72
73 static void tstream_read_packet_done(struct tevent_req *subreq)
74 {
75         struct tevent_req *req = tevent_req_callback_data(
76                 subreq, struct tevent_req);
77         struct tstream_read_packet_state *state = tevent_req_data(
78                 req, struct tstream_read_packet_state);
79         int ret, err;
80         size_t total;
81         ssize_t more;
82         uint8_t *tmp;
83
84         ret = tstream_readv_recv(subreq, &err);
85         TALLOC_FREE(subreq);
86         if (ret == 0) {
87                 err = EPIPE;
88         }
89         if (ret <= 0) {
90                 tevent_req_error(req, err);
91                 return;
92         }
93
94         if (state->more == NULL) {
95                 /* Nobody to ask, this is a async read_data */
96                 tevent_req_done(req);
97                 return;
98         }
99         total = talloc_array_length(state->buf);
100
101         more = state->more(state->buf, total, state->private_data);
102         if (more == -1) {
103                 /* We got an invalid packet, tell the caller */
104                 tevent_req_error(req, EIO);
105                 return;
106         }
107         if (more == 0) {
108                 /* We're done, full packet received */
109                 tevent_req_done(req);
110                 return;
111         }
112
113         tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
114         if (tevent_req_nomem(tmp, req)) {
115                 return;
116         }
117         state->buf = tmp;
118
119         state->iov.iov_base = (void *)(state->buf + total);
120         state->iov.iov_len = more;
121
122         subreq = tstream_readv_send(state, state->ev, state->stream,
123                                     &state->iov, 1);
124         if (tevent_req_nomem(subreq, req)) {
125                 return;
126         }
127         tevent_req_set_callback(subreq, tstream_read_packet_done, req);
128 }
129
130 ssize_t tstream_read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
131                                  uint8_t **pbuf, int *perrno)
132 {
133         struct tstream_read_packet_state *state =
134                 tevent_req_data(req, struct tstream_read_packet_state);
135
136         if (tevent_req_is_unix_error(req, perrno)) {
137                 return -1;
138         }
139         *pbuf = talloc_move(mem_ctx, &state->buf);
140         return talloc_array_length(*pbuf);
141 }