f6c92f3385383b21f410811f19a3986bbc2bf148
[ira/wip.git] / libcli / util / tstream.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  Copyright (C) Stefan Metzmacher 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 <tevent.h>
22 #include "system/filesys.h"
23 #include "../lib/tsocket/tsocket.h"
24 #include "../libcli/util/tstream.h"
25 #include "../lib/util/tevent_ntstatus.h"
26
27 struct tstream_read_pdu_blob_state {
28         /* this structs are owned by the caller */
29         struct {
30                 struct tevent_context *ev;
31                 struct tstream_context *stream;
32                 NTSTATUS (*full_fn)(void *private_data,
33                                     DATA_BLOB blob,
34                                     size_t *packet_size);
35                 void *full_private;
36         } caller;
37
38         DATA_BLOB pdu_blob;
39         struct iovec tmp_vector;
40 };
41
42 static void tstream_read_pdu_blob_done(struct tevent_req *subreq);
43
44 struct tevent_req *tstream_read_pdu_blob_send(TALLOC_CTX *mem_ctx,
45                                 struct tevent_context *ev,
46                                 struct tstream_context *stream,
47                                 size_t initial_read_size,
48                                 NTSTATUS (*full_fn)(void *private_data,
49                                                     DATA_BLOB blob,
50                                                     size_t *packet_size),
51                                 void *full_private)
52 {
53         struct tevent_req *req;
54         struct tstream_read_pdu_blob_state *state;
55         struct tevent_req *subreq;
56         uint8_t *buf;
57
58         req = tevent_req_create(mem_ctx, &state,
59                                 struct tstream_read_pdu_blob_state);
60         if (!req) {
61                 return NULL;
62         }
63
64         state->caller.ev                = ev;
65         state->caller.stream            = stream;
66         state->caller.full_fn           = full_fn;
67         state->caller.full_private      = full_private;
68
69         if (initial_read_size == 0) {
70                 tevent_req_error(req, EINVAL);
71                 return tevent_req_post(req, ev);
72         }
73
74         buf = talloc_array(state, uint8_t, initial_read_size);
75         if (tevent_req_nomem(buf, req)) {
76                 return tevent_req_post(req, ev);
77         }
78         state->pdu_blob.data = buf;
79         state->pdu_blob.length = initial_read_size;
80
81         state->tmp_vector.iov_base = buf;
82         state->tmp_vector.iov_len = initial_read_size;
83
84         subreq = tstream_readv_send(state, ev, stream, &state->tmp_vector, 1);
85         if (tevent_req_nomem(subreq, req)) {
86                 return tevent_req_post(req, ev);
87         }
88         tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
89
90         return req;
91 }
92
93 static void tstream_read_pdu_blob_done(struct tevent_req *subreq)
94 {
95         struct tevent_req *req =
96                 tevent_req_callback_data(subreq,
97                 struct tevent_req);
98         struct tstream_read_pdu_blob_state *state =
99                 tevent_req_data(req,
100                 struct tstream_read_pdu_blob_state);
101         ssize_t ret;
102         int sys_errno;
103         size_t pdu_size;
104         NTSTATUS status;
105         uint8_t *buf;
106
107         ret = tstream_readv_recv(subreq, &sys_errno);
108         TALLOC_FREE(subreq);
109         if (ret == -1) {
110                 status = map_nt_error_from_unix(sys_errno);
111                 tevent_req_nterror(req, status);
112                 return;
113         }
114
115         status = state->caller.full_fn(state->caller.full_private,
116                                        state->pdu_blob, &pdu_size);
117         if (NT_STATUS_IS_OK(status)) {
118                 tevent_req_done(req);
119                 return;
120         } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
121                 /* more to get */
122         } else if (!NT_STATUS_IS_OK(status)) {
123                 tevent_req_nterror(req, status);
124                 return;
125         }
126
127         buf = talloc_realloc(state, state->pdu_blob.data, uint8_t, pdu_size);
128         if (tevent_req_nomem(buf, req)) {
129                 return;
130         }
131         state->pdu_blob.data = buf;
132         state->pdu_blob.length = pdu_size;
133
134         state->tmp_vector.iov_base = buf + state->tmp_vector.iov_len;
135         state->tmp_vector.iov_len = pdu_size - state->tmp_vector.iov_len;
136
137         subreq = tstream_readv_send(state,
138                                     state->caller.ev,
139                                     state->caller.stream,
140                                     &state->tmp_vector,
141                                     1);
142         if (tevent_req_nomem(subreq, req)) {
143                 return;
144         }
145         tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
146 }
147
148 NTSTATUS tstream_read_pdu_blob_recv(struct tevent_req *req,
149                                     TALLOC_CTX *mem_ctx,
150                                     DATA_BLOB *pdu_blob)
151 {
152         struct tstream_read_pdu_blob_state *state = tevent_req_data(req,
153                                         struct tstream_read_pdu_blob_state);
154         NTSTATUS status;
155
156         if (tevent_req_is_nterror(req, &status)) {
157                 tevent_req_received(req);
158                 return status;
159         }
160
161         *pdu_blob = state->pdu_blob;
162         talloc_steal(mem_ctx, pdu_blob->data);
163
164         tevent_req_received(req);
165         return NT_STATUS_OK;
166 }
167