s3: Fix infinite loop in NCACN_IP_TCP asa there is no timeout. Assume lsa_pipe_tcp...
[metze/samba/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 async_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 async_req *result;
54         struct tevent_req *subreq;
55         struct rpc_sock_read_state *state;
56         struct timeval endtime;
57
58         if (!async_req_setup(mem_ctx, &result, &state,
59                              struct rpc_sock_read_state)) {
60                 return NULL;
61         }
62         if (sock_transp->fd == -1) {
63                 if (!async_post_ntstatus(result, ev, NT_STATUS_CONNECTION_INVALID)) {
64                         goto fail;
65                 }
66                 return result;
67         }
68         state->transp = sock_transp;
69         endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
70         subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
71         if (subreq == NULL) {
72                 goto fail;
73         }
74
75         if (!tevent_req_set_endtime(subreq, ev, endtime)) {
76                 goto fail;
77         }
78
79         tevent_req_set_callback(subreq, rpc_sock_read_done, result);
80         return result;
81  fail:
82         TALLOC_FREE(result);
83         return NULL;
84 }
85
86 static void rpc_sock_read_done(struct tevent_req *subreq)
87 {
88         struct async_req *req =
89                 tevent_req_callback_data(subreq, struct async_req);
90         struct rpc_sock_read_state *state = talloc_get_type_abort(
91                 req->private_data, struct rpc_sock_read_state);
92         int err;
93
94         /* We must free subreq in this function as there is
95           a timer event attached to it. */
96
97         state->received = async_recv_recv(subreq, &err);
98
99         if (state->received == -1) {
100                 if (state->transp->fd != -1) {
101                         close(state->transp->fd);
102                         state->transp->fd = -1;
103                 }
104                 TALLOC_FREE(subreq);
105                 async_req_nterror(req, map_nt_error_from_unix(err));
106                 return;
107         }
108         TALLOC_FREE(subreq);
109         async_req_done(req);
110 }
111
112 static NTSTATUS rpc_sock_read_recv(struct async_req *req, ssize_t *preceived)
113 {
114         struct rpc_sock_read_state *state = talloc_get_type_abort(
115                 req->private_data, struct rpc_sock_read_state);
116         NTSTATUS status;
117
118         if (async_req_is_nterror(req, &status)) {
119                 return status;
120         }
121         *preceived = state->received;
122         return NT_STATUS_OK;
123 }
124
125 struct rpc_sock_write_state {
126         struct rpc_transport_sock_state *transp;
127         ssize_t sent;
128 };
129
130 static void rpc_sock_write_done(struct tevent_req *subreq);
131
132 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
133                                              struct event_context *ev,
134                                              const uint8_t *data, size_t size,
135                                              void *priv)
136 {
137         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
138                 priv, struct rpc_transport_sock_state);
139         struct async_req *result;
140         struct tevent_req *subreq;
141         struct rpc_sock_write_state *state;
142         struct timeval endtime;
143
144         if (!async_req_setup(mem_ctx, &result, &state,
145                              struct rpc_sock_write_state)) {
146                 return NULL;
147         }
148         if (sock_transp->fd == -1) {
149                 if (!async_post_ntstatus(result, ev, NT_STATUS_CONNECTION_INVALID)) {
150                         goto fail;
151                 }
152                 return result;
153         }
154         state->transp = sock_transp;
155         endtime = timeval_current_ofs(0, sock_transp->timeout * 1000);
156         subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
157         if (subreq == NULL) {
158                 goto fail;
159         }
160
161         if (!tevent_req_set_endtime(subreq, ev, endtime)) {
162                 goto fail;
163         }
164
165         tevent_req_set_callback(subreq, rpc_sock_write_done, result);
166         return result;
167  fail:
168         TALLOC_FREE(result);
169         return NULL;
170 }
171
172 static void rpc_sock_write_done(struct tevent_req *subreq)
173 {
174         struct async_req *req =
175                 tevent_req_callback_data(subreq, struct async_req);
176         struct rpc_sock_write_state *state = talloc_get_type_abort(
177                 req->private_data, struct rpc_sock_write_state);
178         int err;
179
180         /* We must free subreq in this function as there is
181           a timer event attached to it. */
182
183         state->sent = async_send_recv(subreq, &err);
184
185         if (state->sent == -1) {
186                 if (state->transp->fd != -1) {
187                         close(state->transp->fd);
188                         state->transp->fd = -1;
189                 }
190                 TALLOC_FREE(subreq);
191                 async_req_nterror(req, map_nt_error_from_unix(err));
192                 return;
193         }
194         TALLOC_FREE(subreq);
195         async_req_done(req);
196 }
197
198 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
199 {
200         struct rpc_sock_write_state *state = talloc_get_type_abort(
201                 req->private_data, struct rpc_sock_write_state);
202         NTSTATUS status;
203
204         if (async_req_is_nterror(req, &status)) {
205                 return status;
206         }
207         *psent = state->sent;
208         return NT_STATUS_OK;
209 }
210
211 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
212                                  struct rpc_cli_transport **presult)
213 {
214         struct rpc_cli_transport *result;
215         struct rpc_transport_sock_state *state;
216
217         result = talloc(mem_ctx, struct rpc_cli_transport);
218         if (result == NULL) {
219                 return NT_STATUS_NO_MEMORY;
220         }
221         state = talloc(result, struct rpc_transport_sock_state);
222         if (state == NULL) {
223                 TALLOC_FREE(result);
224                 return NT_STATUS_NO_MEMORY;
225         }
226         result->priv = state;
227
228         state->fd = fd;
229         state->timeout = 10000; /* 10 seconds. */
230         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
231
232         result->trans_send = NULL;
233         result->trans_recv = NULL;
234         result->write_send = rpc_sock_write_send;
235         result->write_recv = rpc_sock_write_recv;
236         result->read_send = rpc_sock_read_send;
237         result->read_recv = rpc_sock_read_recv;
238
239         *presult = result;
240         return NT_STATUS_OK;
241 }