1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | Copyright (C) Volker Lendecke 2005
|
---|
5 | Copyright (C) Andrew Tridgell 2005
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | /*
|
---|
21 | composite API helper functions
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "includes.h"
|
---|
25 | #include "lib/events/events.h"
|
---|
26 | #include "libcli/raw/libcliraw.h"
|
---|
27 | #include "libcli/smb2/smb2.h"
|
---|
28 | #include "libcli/composite/composite.h"
|
---|
29 | #include "../libcli/nbt/libnbt.h"
|
---|
30 |
|
---|
31 | /*
|
---|
32 | create a new composite_context structure
|
---|
33 | and initialize it
|
---|
34 | */
|
---|
35 | _PUBLIC_ struct composite_context *composite_create(TALLOC_CTX *mem_ctx,
|
---|
36 | struct tevent_context *ev)
|
---|
37 | {
|
---|
38 | struct composite_context *c;
|
---|
39 |
|
---|
40 | c = talloc_zero(mem_ctx, struct composite_context);
|
---|
41 | if (!c) return NULL;
|
---|
42 | c->state = COMPOSITE_STATE_IN_PROGRESS;
|
---|
43 | c->event_ctx = ev;
|
---|
44 |
|
---|
45 | return c;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /*
|
---|
49 | block until a composite function has completed, then return the status
|
---|
50 | */
|
---|
51 | _PUBLIC_ NTSTATUS composite_wait(struct composite_context *c)
|
---|
52 | {
|
---|
53 | if (c == NULL) return NT_STATUS_NO_MEMORY;
|
---|
54 |
|
---|
55 | c->used_wait = true;
|
---|
56 |
|
---|
57 | while (c->state < COMPOSITE_STATE_DONE) {
|
---|
58 | if (tevent_loop_once(c->event_ctx) != 0) {
|
---|
59 | return NT_STATUS_UNSUCCESSFUL;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | return c->status;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | block until a composite function has completed, then return the status.
|
---|
68 | Free the composite context before returning
|
---|
69 | */
|
---|
70 | _PUBLIC_ NTSTATUS composite_wait_free(struct composite_context *c)
|
---|
71 | {
|
---|
72 | NTSTATUS status = composite_wait(c);
|
---|
73 | talloc_free(c);
|
---|
74 | return status;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | callback from composite_done() and composite_error()
|
---|
79 |
|
---|
80 | this is used to allow for a composite function to complete without
|
---|
81 | going through any state transitions. When that happens the caller
|
---|
82 | has had no opportunity to fill in the async callback fields
|
---|
83 | (ctx->async.fn and ctx->async.private_data) which means the usual way of
|
---|
84 | dealing with composite functions doesn't work. To cope with this,
|
---|
85 | we trigger a timer event that will happen then the event loop is
|
---|
86 | re-entered. This gives the caller a chance to setup the callback,
|
---|
87 | and allows the caller to ignore the fact that the composite
|
---|
88 | function completed early
|
---|
89 | */
|
---|
90 | static void composite_trigger(struct tevent_context *ev, struct tevent_timer *te,
|
---|
91 | struct timeval t, void *ptr)
|
---|
92 | {
|
---|
93 | struct composite_context *c = talloc_get_type(ptr, struct composite_context);
|
---|
94 | if (c->async.fn) {
|
---|
95 | c->async.fn(c);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
|
---|
101 | {
|
---|
102 | /* you are allowed to pass NT_STATUS_OK to composite_error(), in which
|
---|
103 | case it is equivalent to composite_done() */
|
---|
104 | if (NT_STATUS_IS_OK(status)) {
|
---|
105 | composite_done(ctx);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | if (!ctx->used_wait && !ctx->async.fn) {
|
---|
109 | tevent_add_timer(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
|
---|
110 | }
|
---|
111 | ctx->status = status;
|
---|
112 | ctx->state = COMPOSITE_STATE_ERROR;
|
---|
113 | if (ctx->async.fn != NULL) {
|
---|
114 | ctx->async.fn(ctx);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | _PUBLIC_ bool composite_nomem(const void *p, struct composite_context *ctx)
|
---|
119 | {
|
---|
120 | if (p != NULL) {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | composite_error(ctx, NT_STATUS_NO_MEMORY);
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | _PUBLIC_ bool composite_is_ok(struct composite_context *ctx)
|
---|
128 | {
|
---|
129 | if (NT_STATUS_IS_OK(ctx->status)) {
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 | composite_error(ctx, ctx->status);
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | _PUBLIC_ void composite_done(struct composite_context *ctx)
|
---|
137 | {
|
---|
138 | if (!ctx->used_wait && !ctx->async.fn) {
|
---|
139 | tevent_add_timer(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
|
---|
140 | }
|
---|
141 | ctx->state = COMPOSITE_STATE_DONE;
|
---|
142 | if (ctx->async.fn != NULL) {
|
---|
143 | ctx->async.fn(ctx);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | _PUBLIC_ void composite_continue(struct composite_context *ctx,
|
---|
148 | struct composite_context *new_ctx,
|
---|
149 | void (*continuation)(struct composite_context *),
|
---|
150 | void *private_data)
|
---|
151 | {
|
---|
152 | if (composite_nomem(new_ctx, ctx)) return;
|
---|
153 | new_ctx->async.fn = continuation;
|
---|
154 | new_ctx->async.private_data = private_data;
|
---|
155 |
|
---|
156 | /* if we are setting up a continuation, and the context has
|
---|
157 | already finished, then we should run the callback with an
|
---|
158 | immediate event, otherwise we can be stuck forever */
|
---|
159 | if (new_ctx->state >= COMPOSITE_STATE_DONE && continuation) {
|
---|
160 | tevent_add_timer(new_ctx->event_ctx, new_ctx, timeval_zero(), composite_trigger, new_ctx);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
|
---|
165 | struct smbcli_request *new_req,
|
---|
166 | void (*continuation)(struct smbcli_request *),
|
---|
167 | void *private_data)
|
---|
168 | {
|
---|
169 | if (composite_nomem(new_req, ctx)) return;
|
---|
170 | if (new_req->state > SMBCLI_REQUEST_RECV) {
|
---|
171 | composite_error(ctx, new_req->status);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 | new_req->async.fn = continuation;
|
---|
175 | new_req->async.private_data = private_data;
|
---|
176 | }
|
---|
177 |
|
---|
178 | _PUBLIC_ void composite_continue_smb2(struct composite_context *ctx,
|
---|
179 | struct smb2_request *new_req,
|
---|
180 | void (*continuation)(struct smb2_request *),
|
---|
181 | void *private_data)
|
---|
182 | {
|
---|
183 | if (composite_nomem(new_req, ctx)) return;
|
---|
184 | if (new_req->state > SMB2_REQUEST_RECV) {
|
---|
185 | composite_error(ctx, new_req->status);
|
---|
186 | return;
|
---|
187 | }
|
---|
188 | new_req->async.fn = continuation;
|
---|
189 | new_req->async.private_data = private_data;
|
---|
190 | }
|
---|
191 |
|
---|
192 | _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
|
---|
193 | struct nbt_name_request *new_req,
|
---|
194 | void (*continuation)(struct nbt_name_request *),
|
---|
195 | void *private_data)
|
---|
196 | {
|
---|
197 | if (composite_nomem(new_req, ctx)) return;
|
---|
198 | new_req->async.fn = continuation;
|
---|
199 | new_req->async.private_data = private_data;
|
---|
200 | }
|
---|