Fix broken pipe handling
[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         struct rpc_transport_sock_state *transp;
40         ssize_t received;
41 };
42
43 static void rpc_sock_read_done(struct tevent_req *subreq);
44
45 static struct tevent_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
46                                              struct event_context *ev,
47                                              uint8_t *data, size_t size,
48                                              void *priv)
49 {
50         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
51                 priv, struct rpc_transport_sock_state);
52         struct tevent_req *req, *subreq;
53         struct rpc_sock_read_state *state;
54
55         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_read_state);
56         if (req == NULL) {
57                 return NULL;
58         }
59         if (sock_transp->fd == -1) {
60                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
61                 return tevent_req_post(req, ev);
62         }
63         state->transp = sock_transp;
64         subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
65         if (subreq == NULL) {
66                 goto fail;
67         }
68         tevent_req_set_callback(subreq, rpc_sock_read_done, req);
69         return req;
70  fail:
71         TALLOC_FREE(req);
72         return NULL;
73 }
74
75 static void rpc_sock_read_done(struct tevent_req *subreq)
76 {
77         struct tevent_req *req = tevent_req_callback_data(
78                 subreq, struct tevent_req);
79         struct rpc_sock_read_state *state = tevent_req_data(
80                 req, struct rpc_sock_read_state);
81         int err;
82
83         state->received = async_recv_recv(subreq, &err);
84         if (state->received == -1) {
85                 if (state->transp->fd != -1) {
86                         close(state->transp->fd);
87                         state->transp->fd = -1;
88                 }
89                 tevent_req_nterror(req, map_nt_error_from_unix(err));
90                 return;
91         }
92         tevent_req_done(req);
93 }
94
95 static NTSTATUS rpc_sock_read_recv(struct tevent_req *req, ssize_t *preceived)
96 {
97         struct rpc_sock_read_state *state = tevent_req_data(
98                 req, struct rpc_sock_read_state);
99         NTSTATUS status;
100
101         if (tevent_req_is_nterror(req, &status)) {
102                 return status;
103         }
104         *preceived = state->received;
105         return NT_STATUS_OK;
106 }
107
108 struct rpc_sock_write_state {
109         struct rpc_transport_sock_state *transp;
110         ssize_t sent;
111 };
112
113 static void rpc_sock_write_done(struct tevent_req *subreq);
114
115 static struct tevent_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
116                                               struct event_context *ev,
117                                               const uint8_t *data, size_t size,
118                                               void *priv)
119 {
120         struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
121                 priv, struct rpc_transport_sock_state);
122         struct tevent_req *req, *subreq;
123         struct rpc_sock_write_state *state;
124
125         req = tevent_req_create(mem_ctx, &state, struct rpc_sock_write_state);
126         if (req == NULL) {
127                 return NULL;
128         }
129         if (sock_transp->fd == -1) {
130                 tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
131                 return tevent_req_post(req, ev);
132         }
133         state->transp = sock_transp;
134         subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
135         if (subreq == NULL) {
136                 goto fail;
137         }
138         tevent_req_set_callback(subreq, rpc_sock_write_done, req);
139         return req;
140  fail:
141         TALLOC_FREE(req);
142         return NULL;
143 }
144
145 static void rpc_sock_write_done(struct tevent_req *subreq)
146 {
147         struct tevent_req *req = tevent_req_callback_data(
148                 subreq, struct tevent_req);
149         struct rpc_sock_write_state *state = tevent_req_data(
150                 req, struct rpc_sock_write_state);
151         int err;
152
153         state->sent = async_send_recv(subreq, &err);
154         if (state->sent == -1) {
155                 if (state->transp->fd != -1) {
156                         close(state->transp->fd);
157                         state->transp->fd = -1;
158                 }
159                 tevent_req_nterror(req, map_nt_error_from_unix(err));
160                 return;
161         }
162         tevent_req_done(req);
163 }
164
165 static NTSTATUS rpc_sock_write_recv(struct tevent_req *req, ssize_t *psent)
166 {
167         struct rpc_sock_write_state *state = tevent_req_data(
168                 req, struct rpc_sock_write_state);
169         NTSTATUS status;
170
171         if (tevent_req_is_nterror(req, &status)) {
172                 return status;
173         }
174         *psent = state->sent;
175         return NT_STATUS_OK;
176 }
177
178 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
179                                  struct rpc_cli_transport **presult)
180 {
181         struct rpc_cli_transport *result;
182         struct rpc_transport_sock_state *state;
183
184         result = talloc(mem_ctx, struct rpc_cli_transport);
185         if (result == NULL) {
186                 return NT_STATUS_NO_MEMORY;
187         }
188         state = talloc(result, struct rpc_transport_sock_state);
189         if (state == NULL) {
190                 TALLOC_FREE(result);
191                 return NT_STATUS_NO_MEMORY;
192         }
193         result->priv = state;
194
195         state->fd = fd;
196         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
197
198         result->trans_send = NULL;
199         result->trans_recv = NULL;
200         result->write_send = rpc_sock_write_send;
201         result->write_recv = rpc_sock_write_recv;
202         result->read_send = rpc_sock_read_send;
203         result->read_recv = rpc_sock_read_recv;
204
205         *presult = result;
206         return NT_STATUS_OK;
207 }