Merge branch 'master' of git://git.samba.org/samba
[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         int timeout;
28 };
29
30 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
31 {
32         if (s->fd != -1) {
33                 close(s->fd);
34                 s->fd = -1;
35         }
36         return 0;
37 }
38
39 struct rpc_sock_read_state {
40         struct rpc_transport_sock_state *transp;
41         ssize_t received;
42 };
43
44 static void rpc_sock_read_done(struct tevent_req *subreq);
45
46 static struct tevent_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
47                                              struct event_context *ev,
48                                              uint8_t *data, size_t size,
49                                              void *priv)
50 {
51         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
52                 priv, struct rpc_transport_sock_state);
53         struct tevent_req *req, *subreq;
54         struct rpc_sock_read_state *state;
55         struct timeval endtime;
56
57         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_read_state);
58         if (req == NULL) {
59                 return NULL;
60         }
61         if (sock_transp->fd == -1) {
62                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
63                 return tevent_req_post(req, ev);
64         }
65         state->transp = sock_transp;
66         endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
67         subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
68         if (subreq == NULL) {
69                 goto fail;
70         }
71
72         if (!tevent_req_set_endtime(subreq, ev, endtime)) {
73                 goto fail;
74         }
75
76         tevent_req_set_callback(subreq, rpc_sock_read_done, req);
77         return req;
78  fail:
79         TALLOC_FREE(req);
80         return NULL;
81 }
82
83 static void rpc_sock_read_done(struct tevent_req *subreq)
84 {
85         struct tevent_req *req = tevent_req_callback_data(
86                 subreq, struct tevent_req);
87         struct rpc_sock_read_state *state = tevent_req_data(
88                 req, struct rpc_sock_read_state);
89         int err;
90
91         state->received = async_recv_recv(subreq, &err);
92         if (state->received == -1) {
93                 if (state->transp->fd != -1) {
94                         close(state->transp->fd);
95                         state->transp->fd = -1;
96                 }
97                 tevent_req_nterror(req, map_nt_error_from_unix(err));
98                 return;
99         }
100         tevent_req_done(req);
101 }
102
103 static NTSTATUS rpc_sock_read_recv(struct tevent_req *req, ssize_t *preceived)
104 {
105         struct rpc_sock_read_state *state = tevent_req_data(
106                 req, struct rpc_sock_read_state);
107         NTSTATUS status;
108
109         if (tevent_req_is_nterror(req, &status)) {
110                 return status;
111         }
112         *preceived = state->received;
113         return NT_STATUS_OK;
114 }
115
116 struct rpc_sock_write_state {
117         struct rpc_transport_sock_state *transp;
118         ssize_t sent;
119 };
120
121 static void rpc_sock_write_done(struct tevent_req *subreq);
122
123 static struct tevent_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
124                                               struct event_context *ev,
125                                               const uint8_t *data, size_t size,
126                                               void *priv)
127 {
128         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
129                 priv, struct rpc_transport_sock_state);
130         struct tevent_req *req, *subreq;
131         struct rpc_sock_write_state *state;
132         struct timeval endtime;
133
134         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_write_state);
135         if (req == NULL) {
136                 return NULL;
137         }
138         if (sock_transp->fd == -1) {
139                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
140                 return tevent_req_post(req, ev);
141         }
142         state->transp = sock_transp;
143         endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
144         subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
145         if (subreq == NULL) {
146                 goto fail;
147         }
148
149         if (!tevent_req_set_endtime(subreq, ev, endtime)) {
150                 goto fail;
151         }
152
153         tevent_req_set_callback(subreq, rpc_sock_write_done, req);
154         return req;
155  fail:
156         TALLOC_FREE(req);
157         return NULL;
158 }
159
160 static void rpc_sock_write_done(struct tevent_req *subreq)
161 {
162         struct tevent_req *req = tevent_req_callback_data(
163                 subreq, struct tevent_req);
164         struct rpc_sock_write_state *state = tevent_req_data(
165                 req, struct rpc_sock_write_state);
166         int err;
167
168         state->sent = async_send_recv(subreq, &err);
169         if (state->sent == -1) {
170                 if (state->transp->fd != -1) {
171                         close(state->transp->fd);
172                         state->transp->fd = -1;
173                 }
174                 tevent_req_nterror(req, map_nt_error_from_unix(err));
175                 return;
176         }
177         tevent_req_done(req);
178 }
179
180 static NTSTATUS rpc_sock_write_recv(struct tevent_req *req, ssize_t *psent)
181 {
182         struct rpc_sock_write_state *state = tevent_req_data(
183                 req, struct rpc_sock_write_state);
184         NTSTATUS status;
185
186         if (tevent_req_is_nterror(req, &status)) {
187                 return status;
188         }
189         *psent = state->sent;
190         return NT_STATUS_OK;
191 }
192
193 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
194                                  struct rpc_cli_transport **presult)
195 {
196         struct rpc_cli_transport *result;
197         struct rpc_transport_sock_state *state;
198
199         result = talloc(mem_ctx, struct rpc_cli_transport);
200         if (result == NULL) {
201                 return NT_STATUS_NO_MEMORY;
202         }
203         state = talloc(result, struct rpc_transport_sock_state);
204         if (state == NULL) {
205                 TALLOC_FREE(result);
206                 return NT_STATUS_NO_MEMORY;
207         }
208         result->priv = state;
209
210         state->fd = fd;
211         state->timeout = 10000; /* 10 seconds. */
212         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
213
214         result->trans_send = NULL;
215         result->trans_recv = NULL;
216         result->write_send = rpc_sock_write_send;
217         result->write_recv = rpc_sock_write_recv;
218         result->read_send = rpc_sock_read_send;
219         result->read_recv = rpc_sock_read_recv;
220
221         *presult = result;
222         return NT_STATUS_OK;
223 }
224
225 int rpccli_set_sock_timeout(struct rpc_pipe_client *cli, int timeout)
226 {
227         struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
228                                                         struct rpc_transport_sock_state);
229         int orig_timeout;
230         if (!state) {
231                 return 0;
232         }
233         orig_timeout = state->timeout;
234         state->timeout = timeout;
235         return orig_timeout;
236 }
237
238 void rpccli_close_sock_fd(struct rpc_pipe_client *cli)
239 {
240         struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
241                                                         struct rpc_transport_sock_state);
242         if (state) {
243                 if (state->fd != -1) {
244                         close(state->fd);
245                         state->fd = -1;
246                 }
247         }
248         return;
249 }
250
251 bool rpc_pipe_tcp_connection_ok(struct rpc_pipe_client *cli)
252 {
253         struct rpc_transport_sock_state *state = talloc_get_type(cli->transport->priv,
254                                                         struct rpc_transport_sock_state);
255         if (state && state->fd != -1) {
256                 return true;
257         }
258
259         return false;
260 }