2 * Unix SMB/CIFS implementation.
3 * RPC client transport over a socket
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
21 #include "../lib/async_req/async_sock.h"
24 #define DBGC_CLASS DBGC_RPC_CLI
26 struct rpc_transport_sock_state
{
31 static void rpc_sock_disconnect(struct rpc_transport_sock_state
*s
)
39 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state
*s
)
41 rpc_sock_disconnect(s
);
45 static bool rpc_sock_is_connected(void *priv
)
47 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
48 priv
, struct rpc_transport_sock_state
);
50 if (sock_transp
->fd
== -1) {
57 static unsigned int rpc_sock_set_timeout(void *priv
, unsigned int timeout
)
59 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
60 priv
, struct rpc_transport_sock_state
);
64 ok
= rpc_sock_is_connected(sock_transp
);
69 orig_timeout
= sock_transp
->timeout
;
71 sock_transp
->timeout
= timeout
;
76 struct rpc_sock_read_state
{
77 struct rpc_transport_sock_state
*transp
;
81 static void rpc_sock_read_done(struct tevent_req
*subreq
);
83 static struct tevent_req
*rpc_sock_read_send(TALLOC_CTX
*mem_ctx
,
84 struct event_context
*ev
,
85 uint8_t *data
, size_t size
,
88 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
89 priv
, struct rpc_transport_sock_state
);
90 struct tevent_req
*req
, *subreq
;
91 struct rpc_sock_read_state
*state
;
92 struct timeval endtime
;
94 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_read_state
);
98 if (!rpc_sock_is_connected(sock_transp
)) {
99 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
100 return tevent_req_post(req
, ev
);
102 state
->transp
= sock_transp
;
103 endtime
= timeval_current_ofs(0, sock_transp
->timeout
* 1000);
104 subreq
= async_recv_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
105 if (subreq
== NULL
) {
109 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
113 tevent_req_set_callback(subreq
, rpc_sock_read_done
, req
);
120 static void rpc_sock_read_done(struct tevent_req
*subreq
)
122 struct tevent_req
*req
= tevent_req_callback_data(
123 subreq
, struct tevent_req
);
124 struct rpc_sock_read_state
*state
= tevent_req_data(
125 req
, struct rpc_sock_read_state
);
128 /* We must free subreq in this function as there is
129 a timer event attached to it. */
131 state
->received
= async_recv_recv(subreq
, &err
);
133 if (state
->received
== -1) {
135 rpc_sock_disconnect(state
->transp
);
136 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
140 tevent_req_done(req
);
143 static NTSTATUS
rpc_sock_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
145 struct rpc_sock_read_state
*state
= tevent_req_data(
146 req
, struct rpc_sock_read_state
);
149 if (tevent_req_is_nterror(req
, &status
)) {
152 *preceived
= state
->received
;
156 struct rpc_sock_write_state
{
157 struct rpc_transport_sock_state
*transp
;
161 static void rpc_sock_write_done(struct tevent_req
*subreq
);
163 static struct tevent_req
*rpc_sock_write_send(TALLOC_CTX
*mem_ctx
,
164 struct event_context
*ev
,
165 const uint8_t *data
, size_t size
,
168 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
169 priv
, struct rpc_transport_sock_state
);
170 struct tevent_req
*req
, *subreq
;
171 struct rpc_sock_write_state
*state
;
172 struct timeval endtime
;
174 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_write_state
);
178 if (!rpc_sock_is_connected(sock_transp
)) {
179 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
180 return tevent_req_post(req
, ev
);
182 state
->transp
= sock_transp
;
183 endtime
= timeval_current_ofs(0, sock_transp
->timeout
* 1000);
184 subreq
= async_send_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
185 if (subreq
== NULL
) {
189 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
193 tevent_req_set_callback(subreq
, rpc_sock_write_done
, req
);
200 static void rpc_sock_write_done(struct tevent_req
*subreq
)
202 struct tevent_req
*req
= tevent_req_callback_data(
203 subreq
, struct tevent_req
);
204 struct rpc_sock_write_state
*state
= tevent_req_data(
205 req
, struct rpc_sock_write_state
);
208 /* We must free subreq in this function as there is
209 a timer event attached to it. */
211 state
->sent
= async_send_recv(subreq
, &err
);
213 if (state
->sent
== -1) {
215 rpc_sock_disconnect(state
->transp
);
216 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
220 tevent_req_done(req
);
223 static NTSTATUS
rpc_sock_write_recv(struct tevent_req
*req
, ssize_t
*psent
)
225 struct rpc_sock_write_state
*state
= tevent_req_data(
226 req
, struct rpc_sock_write_state
);
229 if (tevent_req_is_nterror(req
, &status
)) {
232 *psent
= state
->sent
;
236 NTSTATUS
rpc_transport_sock_init(TALLOC_CTX
*mem_ctx
, int fd
,
237 struct rpc_cli_transport
**presult
)
239 struct rpc_cli_transport
*result
;
240 struct rpc_transport_sock_state
*state
;
242 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
243 if (result
== NULL
) {
244 return NT_STATUS_NO_MEMORY
;
246 state
= talloc(result
, struct rpc_transport_sock_state
);
249 return NT_STATUS_NO_MEMORY
;
251 result
->priv
= state
;
254 state
->timeout
= 10000; /* 10 seconds. */
255 talloc_set_destructor(state
, rpc_transport_sock_state_destructor
);
257 result
->trans_send
= NULL
;
258 result
->trans_recv
= NULL
;
259 result
->write_send
= rpc_sock_write_send
;
260 result
->write_recv
= rpc_sock_write_recv
;
261 result
->read_send
= rpc_sock_read_send
;
262 result
->read_recv
= rpc_sock_read_recv
;
263 result
->is_connected
= rpc_sock_is_connected
;
264 result
->set_timeout
= rpc_sock_set_timeout
;