2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2005
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for loading a whole file into memory
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
28 enum fetchfile_stage {FETCHFILE_CONNECT,
31 struct fetchfile_state {
32 enum fetchfile_stage stage;
33 struct smb_composite_fetchfile *io;
34 struct composite_context *creq;
35 struct smb_composite_connect *connect;
36 struct smb_composite_loadfile *loadfile;
39 static void fetchfile_composite_handler(struct composite_context *req);
41 static NTSTATUS fetchfile_connect(struct composite_context *c,
42 struct smb_composite_fetchfile *io)
45 struct fetchfile_state *state;
46 state = talloc_get_type(c->private_data, struct fetchfile_state);
48 status = smb_composite_connect_recv(state->creq, c);
49 NT_STATUS_NOT_OK_RETURN(status);
51 state->loadfile = talloc(state, struct smb_composite_loadfile);
52 NT_STATUS_HAVE_NO_MEMORY(state->loadfile);
54 state->loadfile->in.fname = io->in.filename;
56 state->creq = smb_composite_loadfile_send(state->connect->out.tree,
58 NT_STATUS_HAVE_NO_MEMORY(state->creq);
60 state->creq->async.private_data = c;
61 state->creq->async.fn = fetchfile_composite_handler;
63 state->stage = FETCHFILE_READ;
64 c->event_ctx = talloc_reference(c, state->creq->event_ctx);
69 static NTSTATUS fetchfile_read(struct composite_context *c,
70 struct smb_composite_fetchfile *io)
73 struct fetchfile_state *state;
74 state = talloc_get_type(c->private_data, struct fetchfile_state);
76 status = smb_composite_loadfile_recv(state->creq, NULL);
77 NT_STATUS_NOT_OK_RETURN(status);
79 io->out.data = state->loadfile->out.data;
80 io->out.size = state->loadfile->out.size;
82 c->state = COMPOSITE_STATE_DONE;
89 static void fetchfile_state_handler(struct composite_context *c)
91 struct fetchfile_state *state;
92 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
94 state = talloc_get_type(c->private_data, struct fetchfile_state);
96 /* when this handler is called, the stage indicates what
97 call has just finished */
98 switch (state->stage) {
99 case FETCHFILE_CONNECT:
100 status = fetchfile_connect(c, state->io);
103 status = fetchfile_read(c, state->io);
107 if (!NT_STATUS_IS_OK(status)) {
109 c->state = COMPOSITE_STATE_ERROR;
116 static void fetchfile_composite_handler(struct composite_context *creq)
118 struct composite_context *c = talloc_get_type(creq->async.private_data,
119 struct composite_context);
120 fetchfile_state_handler(c);
123 struct composite_context *smb_composite_fetchfile_send(struct smb_composite_fetchfile *io,
124 struct event_context *event_ctx)
126 struct composite_context *c;
127 struct fetchfile_state *state;
129 c = talloc_zero(NULL, struct composite_context);
130 if (c == NULL) goto failed;
132 state = talloc(c, struct fetchfile_state);
133 if (state == NULL) goto failed;
135 state->connect = talloc(state, struct smb_composite_connect);
136 if (state->connect == NULL) goto failed;
140 state->connect->in.dest_host = io->in.dest_host;
141 state->connect->in.port = io->in.port;
142 state->connect->in.called_name = io->in.called_name;
143 state->connect->in.service = io->in.service;
144 state->connect->in.service_type = io->in.service_type;
145 state->connect->in.credentials = io->in.credentials;
146 state->connect->in.fallback_to_anonymous = False;
147 state->connect->in.workgroup = io->in.workgroup;
149 state->creq = smb_composite_connect_send(state->connect, state, event_ctx);
150 if (state->creq == NULL) goto failed;
152 state->creq->async.private_data = c;
153 state->creq->async.fn = fetchfile_composite_handler;
155 c->state = COMPOSITE_STATE_IN_PROGRESS;
156 state->stage = FETCHFILE_CONNECT;
157 c->event_ctx = talloc_reference(c, state->creq->event_ctx);
158 c->private_data = state;
166 NTSTATUS smb_composite_fetchfile_recv(struct composite_context *c,
171 status = composite_wait(c);
173 if (NT_STATUS_IS_OK(status)) {
174 struct fetchfile_state *state = talloc_get_type(c->private_data, struct fetchfile_state);
175 talloc_steal(mem_ctx, state->io->out.data);
182 NTSTATUS smb_composite_fetchfile(struct smb_composite_fetchfile *io,
185 struct composite_context *c = smb_composite_fetchfile_send(io, NULL);
186 return smb_composite_fetchfile_recv(c, mem_ctx);