Convert rpc_cli_transport->write to tevent_req
[ira/wip.git] / source3 / rpc_client / rpc_transport_sock.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over a socket
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
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
24
25 struct rpc_transport_sock_state {
26         int fd;
27 };
28
29 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
30 {
31         if (s->fd != -1) {
32                 close(s->fd);
33                 s->fd = -1;
34         }
35         return 0;
36 }
37
38 struct rpc_sock_read_state {
39         ssize_t received;
40 };
41
42 static void rpc_sock_read_done(struct tevent_req *subreq);
43
44 static struct tevent_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
45                                              struct event_context *ev,
46                                              uint8_t *data, size_t size,
47                                              void *priv)
48 {
49         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
50                 priv, struct rpc_transport_sock_state);
51         struct tevent_req *req, *subreq;
52         struct rpc_sock_read_state *state;
53
54         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_read_state);
55         if (req == NULL) {
56                 return NULL;
57         }
58
59         subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
60         if (subreq == NULL) {
61                 goto fail;
62         }
63         tevent_req_set_callback(subreq, rpc_sock_read_done, req);
64         return req;
65  fail:
66         TALLOC_FREE(req);
67         return NULL;
68 }
69
70 static void rpc_sock_read_done(struct tevent_req *subreq)
71 {
72         struct tevent_req *req = tevent_req_callback_data(
73                 subreq, struct tevent_req);
74         struct rpc_sock_read_state *state = tevent_req_data(
75                 req, struct rpc_sock_read_state);
76         int err;
77
78         state->received = async_recv_recv(subreq, &err);
79         if (state->received == -1) {
80                 tevent_req_nterror(req, map_nt_error_from_unix(err));
81                 return;
82         }
83         tevent_req_done(req);
84 }
85
86 static NTSTATUS rpc_sock_read_recv(struct tevent_req *req, ssize_t *preceived)
87 {
88         struct rpc_sock_read_state *state = tevent_req_data(
89                 req, struct rpc_sock_read_state);
90         NTSTATUS status;
91
92         if (tevent_req_is_nterror(req, &status)) {
93                 return status;
94         }
95         *preceived = state->received;
96         return NT_STATUS_OK;
97 }
98
99 struct rpc_sock_write_state {
100         ssize_t sent;
101 };
102
103 static void rpc_sock_write_done(struct tevent_req *subreq);
104
105 static struct tevent_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
106                                               struct event_context *ev,
107                                               const uint8_t *data, size_t size,
108                                               void *priv)
109 {
110         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
111                 priv, struct rpc_transport_sock_state);
112         struct tevent_req *req, *subreq;
113         struct rpc_sock_write_state *state;
114
115         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_write_state);
116         if (req == NULL) {
117                 return NULL;
118         }
119         subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
120         if (subreq == NULL) {
121                 goto fail;
122         }
123         tevent_req_set_callback(subreq, rpc_sock_write_done, req);
124         return req;
125  fail:
126         TALLOC_FREE(req);
127         return NULL;
128 }
129
130 static void rpc_sock_write_done(struct tevent_req *subreq)
131 {
132         struct tevent_req *req = tevent_req_callback_data(
133                 subreq, struct tevent_req);
134         struct rpc_sock_write_state *state = tevent_req_data(
135                 req, struct rpc_sock_write_state);
136         int err;
137
138         state->sent = async_send_recv(subreq, &err);
139         if (state->sent == -1) {
140                 tevent_req_nterror(req, map_nt_error_from_unix(err));
141                 return;
142         }
143         tevent_req_done(req);
144 }
145
146 static NTSTATUS rpc_sock_write_recv(struct tevent_req *req, ssize_t *psent)
147 {
148         struct rpc_sock_write_state *state = tevent_req_data(
149                 req, struct rpc_sock_write_state);
150         NTSTATUS status;
151
152         if (tevent_req_is_nterror(req, &status)) {
153                 return status;
154         }
155         *psent = state->sent;
156         return NT_STATUS_OK;
157 }
158
159 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
160                                  struct rpc_cli_transport **presult)
161 {
162         struct rpc_cli_transport *result;
163         struct rpc_transport_sock_state *state;
164
165         result = talloc(mem_ctx, struct rpc_cli_transport);
166         if (result == NULL) {
167                 return NT_STATUS_NO_MEMORY;
168         }
169         state = talloc(result, struct rpc_transport_sock_state);
170         if (state == NULL) {
171                 TALLOC_FREE(result);
172                 return NT_STATUS_NO_MEMORY;
173         }
174         result->priv = state;
175
176         state->fd = fd;
177         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
178
179         result->trans_send = NULL;
180         result->trans_recv = NULL;
181         result->write_send = rpc_sock_write_send;
182         result->write_recv = rpc_sock_write_recv;
183         result->read_send = rpc_sock_read_send;
184         result->read_recv = rpc_sock_read_recv;
185
186         *presult = result;
187         return NT_STATUS_OK;
188 }