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/>.
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_sock_state
{
30 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state
*s
)
39 struct rpc_sock_read_state
{
40 struct rpc_transport_sock_state
*transp
;
44 static void rpc_sock_read_done(struct tevent_req
*subreq
);
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
,
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
;
57 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_read_state
);
61 if (sock_transp
->fd
== -1) {
62 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
63 return tevent_req_post(req
, ev
);
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);
72 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
76 tevent_req_set_callback(subreq
, rpc_sock_read_done
, req
);
83 static void rpc_sock_read_done(struct tevent_req
*subreq
)
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
);
91 /* We must free subreq in this function as there is
92 a timer event attached to it. */
94 state
->received
= async_recv_recv(subreq
, &err
);
96 if (state
->received
== -1) {
97 if (state
->transp
->fd
!= -1) {
98 close(state
->transp
->fd
);
99 state
->transp
->fd
= -1;
102 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
106 tevent_req_done(req
);
109 static NTSTATUS
rpc_sock_read_recv(struct tevent_req
*req
, ssize_t
*preceived
)
111 struct rpc_sock_read_state
*state
= tevent_req_data(
112 req
, struct rpc_sock_read_state
);
115 if (tevent_req_is_nterror(req
, &status
)) {
118 *preceived
= state
->received
;
122 struct rpc_sock_write_state
{
123 struct rpc_transport_sock_state
*transp
;
127 static void rpc_sock_write_done(struct tevent_req
*subreq
);
129 static struct tevent_req
*rpc_sock_write_send(TALLOC_CTX
*mem_ctx
,
130 struct event_context
*ev
,
131 const uint8_t *data
, size_t size
,
134 struct rpc_transport_sock_state
*sock_transp
= talloc_get_type_abort(
135 priv
, struct rpc_transport_sock_state
);
136 struct tevent_req
*req
, *subreq
;
137 struct rpc_sock_write_state
*state
;
138 struct timeval endtime
;
140 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_sock_write_state
);
144 if (sock_transp
->fd
== -1) {
145 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
146 return tevent_req_post(req
, ev
);
148 state
->transp
= sock_transp
;
149 endtime
= timeval_current_ofs(0, sock_transp
->timeout
* 1000);
150 subreq
= async_send_send(state
, ev
, sock_transp
->fd
, data
, size
, 0);
151 if (subreq
== NULL
) {
155 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
159 tevent_req_set_callback(subreq
, rpc_sock_write_done
, req
);
166 static void rpc_sock_write_done(struct tevent_req
*subreq
)
168 struct tevent_req
*req
= tevent_req_callback_data(
169 subreq
, struct tevent_req
);
170 struct rpc_sock_write_state
*state
= tevent_req_data(
171 req
, struct rpc_sock_write_state
);
174 /* We must free subreq in this function as there is
175 a timer event attached to it. */
177 state
->sent
= async_send_recv(subreq
, &err
);
179 if (state
->sent
== -1) {
180 if (state
->transp
->fd
!= -1) {
181 close(state
->transp
->fd
);
182 state
->transp
->fd
= -1;
185 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
189 tevent_req_done(req
);
192 static NTSTATUS
rpc_sock_write_recv(struct tevent_req
*req
, ssize_t
*psent
)
194 struct rpc_sock_write_state
*state
= tevent_req_data(
195 req
, struct rpc_sock_write_state
);
198 if (tevent_req_is_nterror(req
, &status
)) {
201 *psent
= state
->sent
;
205 NTSTATUS
rpc_transport_sock_init(TALLOC_CTX
*mem_ctx
, int fd
,
206 struct rpc_cli_transport
**presult
)
208 struct rpc_cli_transport
*result
;
209 struct rpc_transport_sock_state
*state
;
211 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
212 if (result
== NULL
) {
213 return NT_STATUS_NO_MEMORY
;
215 state
= talloc(result
, struct rpc_transport_sock_state
);
218 return NT_STATUS_NO_MEMORY
;
220 result
->priv
= state
;
223 state
->timeout
= 10000; /* 10 seconds. */
224 talloc_set_destructor(state
, rpc_transport_sock_state_destructor
);
226 result
->trans_send
= NULL
;
227 result
->trans_recv
= NULL
;
228 result
->write_send
= rpc_sock_write_send
;
229 result
->write_recv
= rpc_sock_write_recv
;
230 result
->read_send
= rpc_sock_read_send
;
231 result
->read_recv
= rpc_sock_read_recv
;
237 int rpccli_set_sock_timeout(struct rpc_pipe_client
*cli
, int timeout
)
239 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
240 struct rpc_transport_sock_state
);
245 orig_timeout
= state
->timeout
;
246 state
->timeout
= timeout
;
250 void rpccli_close_sock_fd(struct rpc_pipe_client
*cli
)
252 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
253 struct rpc_transport_sock_state
);
255 if (state
->fd
!= -1) {
263 bool rpc_pipe_tcp_connection_ok(struct rpc_pipe_client
*cli
)
265 struct rpc_transport_sock_state
*state
= talloc_get_type(cli
->transport
->priv
,
266 struct rpc_transport_sock_state
);
267 if (state
&& state
->fd
!= -1) {