c9fcbcc07e915508f692cee10931621268af924d
[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 async_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 async_req *result;
113         struct tevent_req *subreq;
114         struct rpc_sock_write_state *state;
115
116         if (!async_req_setup(mem_ctx, &result, &state,
117                              struct rpc_sock_write_state)) {
118                 return NULL;
119         }
120         subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
121         if (subreq == NULL) {
122                 goto fail;
123         }
124         tevent_req_set_callback(subreq, rpc_sock_write_done, result);
125         return result;
126  fail:
127         TALLOC_FREE(result);
128         return NULL;
129 }
130
131 static void rpc_sock_write_done(struct tevent_req *subreq)
132 {
133         struct async_req *req =
134                 tevent_req_callback_data(subreq, struct async_req);
135         struct rpc_sock_write_state *state = talloc_get_type_abort(
136                 req->private_data, struct rpc_sock_write_state);
137         int err;
138
139         state->sent = async_send_recv(subreq, &err);
140         if (state->sent == -1) {
141                 async_req_nterror(req, map_nt_error_from_unix(err));
142                 return;
143         }
144         async_req_done(req);
145 }
146
147 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
148 {
149         struct rpc_sock_write_state *state = talloc_get_type_abort(
150                 req->private_data, struct rpc_sock_write_state);
151         NTSTATUS status;
152
153         if (async_req_is_nterror(req, &status)) {
154                 return status;
155         }
156         *psent = state->sent;
157         return NT_STATUS_OK;
158 }
159
160 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
161                                  struct rpc_cli_transport **presult)
162 {
163         struct rpc_cli_transport *result;
164         struct rpc_transport_sock_state *state;
165
166         result = talloc(mem_ctx, struct rpc_cli_transport);
167         if (result == NULL) {
168                 return NT_STATUS_NO_MEMORY;
169         }
170         state = talloc(result, struct rpc_transport_sock_state);
171         if (state == NULL) {
172                 TALLOC_FREE(result);
173                 return NT_STATUS_NO_MEMORY;
174         }
175         result->priv = state;
176
177         state->fd = fd;
178         talloc_set_destructor(state, rpc_transport_sock_state_destructor);
179
180         result->trans_send = NULL;
181         result->trans_recv = NULL;
182         result->write_send = rpc_sock_write_send;
183         result->write_recv = rpc_sock_write_recv;
184         result->read_send = rpc_sock_read_send;
185         result->read_recv = rpc_sock_read_recv;
186
187         *presult = result;
188         return NT_STATUS_OK;
189 }